<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%">
    <el-col><span>{{ treeNode.name }}</span></el-col>
    <el-col :span="4">
      <!-- 右边 两个内容 -->
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 -->
          <el-dropdown @command="handleCommand">
            <!-- 内容 -->
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉选项 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add" :disabled="!checkPermission('add')">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// import { constantRoutes } from '@/router'
import { delDepartments } from '@/api/departments.js' // 删除部门的接口函数
// import { delDepartments, addDepartments } from '@/api/departments.js' // 删除部门的接口函数
export default {
  name: '',
  props: {
    treeNode: { // 企业的当前被点击的部门列表的详细数据 父组件通过调取接口获取而来
      required: true,
      type: Object
    },
    isRoot: {
      default: false,
      type: Boolean
    }
  },
  methods: {
    // 下拉菜单选项 点击添加 编辑 删除部门时触发 与command属性息息相关
    async handleCommand(type) {
      try {
      // console.log(type)
        if (type === 'add') {
        // 添加部门
        // addDepartments()
          // console.log(this.treeNode)
          this.$emit('addDepts', this.treeNode)
        } else if (type === 'edit') {
        // 编辑部门
          this.$emit('editDepts', this.treeNode)
          // console.log(this.treeNode)
        } else {
        // 删除部门
        // console.log('删除')
          await this.$confirm('确认删除')
          // console.log(888)
          await delDepartments(this.treeNode.id) // Promise对象
          this.$emit('delDepts')
          this.$message.success('删除部门成功')
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style>

</style>
